package cj.web.prize.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.constants.MemuEnum;
import cj.web.prize.dao.PrizeDAO;
import cj.web.prize.domain.Prize;

/**
 * 控制活动是否开始
 * @author Administrator
 *
 */
@Path("/activeAdmin")
public class ActiveAdminController {

	private static Logger logger = Logger.getLogger(ActiveAdminController.class.getName());
	
	@Autowired
	private PrizeDAO prizeDao;
	
	@Get("show")
	public String show(Model model) {
		//MemuEnum
		model.add("MemuEnum",MemuEnum.jpglhdkz);
		return "/views/prize/activeAdminList.vm";
	}

	@Post("getList")
	public String queryList(Prize prize) {
	
		List<Prize> arr = prizeDao.query(prize);
		
		for(Prize obj:arr)
		{
			obj.setStartAT(DateUtils.getMinusTime(obj.getStartAT(), DateUtils.DATE_FULL_STR, 0));
			obj.setEndAT(DateUtils.getMinusTime(obj.getEndAT(), DateUtils.DATE_FULL_STR, 0));
		}
		
		JSONObject jo = new JSONObject();

		int size = prizeDao.querySize(prize);

		jo.put("data", arr);
		jo.put("draw", prize.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Prize obj = prizeDao.queryById(id);
		
		obj.setStartAT(DateUtils.getMinusTime(obj.getStartAT(), DateUtils.DATE_FULL_STR, 0));
		obj.setEndAT(DateUtils.getMinusTime(obj.getEndAT(), DateUtils.DATE_FULL_STR, 0));
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
		model.add("MemuEnum",MemuEnum.jpglhdkz);
		return "/views/prize/activeAdminEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Prize prize,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			prizeDao.update(prize.getId(), prize.getStartAT(), prize.getEndAT(), prize.getActiveName());
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
}